package com.srd.phone.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.shengrongdai.srd.R;
import com.srd.phone.bean.BarrioInfo;
import com.srd.phone.utils.wheel.adapter.ArrayWheelAdapter;
import com.srd.phone.utils.wheel.widget.OnWheelChangedListener;
import com.srd.phone.utils.wheel.widget.WheelView;

/**
 * 2016 - 05- 16 3
 */
public class BarrioTwinsDialog extends BarrioDialog implements View.OnClickListener, OnWheelChangedListener {

    private Context mContext;
    private WheelView wvProvince, wvCity;
    private OnAddressCListener mOnAddressCListener;

    public BarrioTwinsDialog(Context context) {
        super(context, R.style.dialog);
    }

    public BarrioTwinsDialog(Context context, OnAddressCListener onAddressCListener) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.mOnAddressCListener = onAddressCListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_addr);
        setUpViews();
        setUpListener();
        setUpData();
    }

    public static BarrioTwinsDialog makeTwinsDialog(Context context, OnAddressCListener onAddressCListener) {
        BarrioTwinsDialog dialog = new BarrioTwinsDialog(context);
        dialog.mContext = context;
        dialog.mOnAddressCListener = onAddressCListener;
        return dialog;
    }


    private void setUpViews() {
        wvProvince = (WheelView) findViewById(R.id.wv_province);
        wvCity = (WheelView) findViewById(R.id.wv_city);
        findViewById(R.id.btn_myinfo_sure).setOnClickListener(this);
        findViewById(R.id.btn_myinfo_cancel).setOnClickListener(this);
    }

    private void setUpListener() {
        wvProvince.addChangingListener(this);
        wvCity.addChangingListener(this);
    }

    private void setUpData() {
        initDatas();
        String[] arr = new String[mProvinceDatas.length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = mProvinceDatas[i].name;
        }
        wvProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext, arr));
        wvProvince.setVisibleItems(7);
        wvCity.setVisibleItems(7);
        updateCities();
    }

    //更新市
    private void updateCities() {
        int pCurrent = wvProvince.getCurrentItem();
        mCurrentProvice = mProvinceDatas[pCurrent];
        BarrioInfo[] cities = mCitisDatasMap.get(mCurrentProvice.id);
        if (cities == null) {
            cities = new BarrioInfo[0];
        }
        String[] value = new String[cities.length];
        for (int i = 0; i < cities.length; i++) {
            value[i] = cities[i].name;
        }
        wvCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, value));
        if (cities.length != 0) {
            wvCity.setCurrentItem(0);
            mCurrentCity = cities[0];
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_myinfo_sure:
                if (mOnAddressCListener != null)
                    mOnAddressCListener.onClick(mCurrentProvice, mCurrentCity);
                dismiss();
                break;

            case R.id.btn_myinfo_cancel:
                dismiss();
                break;
        }
    }

    public void dismiss() {
        mContext = null;
        super.dismiss();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wvProvince) {
            updateCities();
        } else if (wheel == wvCity) {
            int pCurrent = wvCity.getCurrentItem();
            mCurrentCity = mCitisDatasMap.get(mCurrentProvice.id)[pCurrent];
        }
    }

    public interface OnAddressCListener {
        public void onClick(BarrioInfo province, BarrioInfo city);
    }
}
